package com.hanter.rad.widget.cupertino.utils;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;

import com.hanter.pickerview.utils.R;

/**
 * 类名：PickerFragment <br/>
 * 描述：Picker选择的Fragment
 * 创建时间：2019/10/13 14:08
 *
 * @author hanter
 * @version 1.0
 */
public class PickerFragment extends BasePickerFragment implements View.OnClickListener {

    @Nullable
    protected OnPickerClickListener listener;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if (context instanceof PickerProvider) {
            provider = (PickerProvider) context;
        }

        if (context instanceof OnPickerClickListener) {
            listener = (OnPickerClickListener) context;
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnCancel) {
            dismiss();

            if (listener != null) {
                listener.onPickerCancel(getTag());
            }
        } else if (v.getId() == R.id.btnSubmit) {
            dismiss();

            if (listener != null) {
                int[] indexes = new int[pvOptions.getNumberOfComponents()];
                for (int i = 0; i < indexes.length; i++) {
                    indexes[i] = pvOptions.getSelectedIndex(i);
                }
                listener.onPickerSelected(getTag(), indexes);
            }
        } else if (v.getId() == R.id.picker_root_container) {
            dismiss();
        }
    }

    @Nullable
    public OnPickerClickListener getListener() {
        return listener;
    }

    public void setListener(@Nullable OnPickerClickListener listener) {
        this.listener = listener;
    }

}
